import java.util.*;

public class Solution355 {

    Map<Integer,Set<Integer>> follow=new HashMap<>();
    Map<Integer,List<int[]>> post=new HashMap<>();
    int count=0;

    public Solution355() {

    }

    public void postTweet(int userId, int tweetId) {
        post.putIfAbsent(userId,new ArrayList<>());
        List<int[]> list=post.get(userId);
        list.add(new int[]{tweetId,count});
        count++;
    }

    public List<Integer> getNewsFeed(int userId) {
        post.putIfAbsent(userId,new ArrayList<>());
        List<int[]> all = new ArrayList<>(post.get(userId));
        follow.putIfAbsent(userId,new HashSet<>());
        Set<Integer> set=follow.get(userId);
        for (Integer integer : set) {
            post.putIfAbsent(integer,new ArrayList<>());
            all.addAll(post.get(integer));
        }
        all.sort(Comparator.comparingInt(a -> -a[1]));
        List<Integer> ans=new ArrayList<>();
        for (int i=0;i<Math.min(10,all.size());i++){
            ans.add(all.get(i)[0]);
        }
        return ans;
    }

    public void follow(int followerId, int followeeId) {
        follow.putIfAbsent(followerId,new HashSet<>());
        if (followeeId!=followerId) {
            Set<Integer> list = follow.get(followerId);
            list.add(followeeId);
        }
    }

    public void unfollow(int followerId, int followeeId) {
        follow.putIfAbsent(followerId,new HashSet<>());
        if (followeeId!=followerId) {
            Set<Integer> list = follow.get(followerId);
            list.remove(followeeId);
        }
    }

    public static void main(String[] args) {
        Solution355 solution355=new Solution355();
        solution355.postTweet(1,5);
        System.out.println(solution355.getNewsFeed(1));
        solution355.follow(1,2);
        solution355.postTweet(2,6);
        System.out.println(solution355.getNewsFeed(1));
        solution355.unfollow(1,2);
        System.out.println(solution355.getNewsFeed(1));
    }
}
